package com.example.zk2.search

import android.os.Bundle
import android.view.LayoutInflater
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.zk2.R
import com.example.zk2.adapter.GoodsAdapter
import com.example.zk2.databinding.ActivitySearchBinding
import com.example.zk2.room.AppDbUtils
import com.example.zk2.room.History
import com.google.android.material.chip.Chip

class SearchActivity : BaseActivity<ActivitySearchBinding,SearchViewModel>() {
    lateinit var goodsAdapter: GoodsAdapter
    override fun initView() {
        goodsAdapter = GoodsAdapter()
        binding.rv.adapter = goodsAdapter
        binding.rv.layoutManager = GridLayoutManager(this,2)

        binding.sv.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                if(query!=null){
                    viewModel.search(query,10,1)
                    AppDbUtils.getDb().getHisDao().insert(History(query,"123",0))
                    //实时刷新
                    queryAll()
                }
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                return false
            }
        })
        //展示历史搜索记录
        queryAll()
    }

    private fun queryAll() {
        val list = AppDbUtils.getDb().getHisDao().query("123")
        if(list.size>0){
            binding.cg.removeAllViews()//先清空，不然会重复出现
            list.forEach { it->
                //创建chip
                val chip = LayoutInflater.from(this).inflate(R.layout.item_chip, null) as Chip
                //给name 赋值
                chip.findViewById<TextView>(R.id.name).text = it.keyword

                //添加到cg中
                binding.cg.addView(chip)
                //点击事件
                chip.setOnClickListener { it1->
                    binding.sv.setQuery(it.keyword,true)
                }
                //点击关闭图标
                chip.setOnCloseIconClickListener { it2->
                    AppDbUtils.getDb().getHisDao().delete(it)
                    binding.cg.removeView(it2)
                }
            }
        }
    }

    override fun initData() {
        viewModel.searchSuccess.observe(this){
            if(it.code==200){
                goodsAdapter.data.clear()
                goodsAdapter.data.addAll(it.data.list)
                goodsAdapter.notifyDataSetChanged()
                if(it.data.list.size==0){
                    ToastUtils.showLong("没有查找到商品，可以试试其他关键词")
                }
            }
        }
    }

    override fun getViewModel(): Class<SearchViewModel> {
        return SearchViewModel::class.java
    }

    override fun getLayout(): Int {
        return R.layout.activity_search
    }

}